<?php
require dirname(__FILE__) .  '/../libs/CoffeeScript/Init.php';
CoffeeScript\Init::load(); 

class CoffeeScript extends CApplicationComponent {
	public $options = array();
	
	public function compile($alias) {
		if (!YII_DEBUG) return;
		
		$file = Yii::getPathOfAlias($alias);
		$coffee = file_get_contents("{$file}.coffee");
		$js = CoffeeScript\Compiler::compile($coffee, $this->options);
		file_put_contents("{$file}.js", $js, LOCK_EX);
	}
	
    public function registerContent($id, $alias, $pos=4,  $data=null) {
		$this->compile($alias);
		$cs = Yii::app()->clientScript;
		$cs->registerScriptContent($id, $alias, $pos, $data);
    }

}
